$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/adminlist',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'adminId', index: "adminId", width: 45, key: true ,hidden:true},
			{ label: '用户名', name: 'username', width: 75 },
            { label: '账号', name: 'account', width: 75 },
			{ label: 'APP名称', name:'appName', width: 75 },
			{ label: 'OEM', name: 'oemName', width: 75 },
			{ label: '公司', name: 'company', width: 75 },
			{ label: '手机号', name: 'phone', width: 100},
			{ label: '状态', name: 'adminStatus', width: 80, formatter: function(value, options, row){
				return value == 1 ?
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},
			{ label: '创建时间', name: 'createDate', index: "createDate", width: 80}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });


});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList:true,
		title:null,
		roleList:{},
		isUpdate:false,
		user:{},
		loginUser:{}
	},
	methods:{
		add:function () {
			vm.showList = false;
			vm.title = "新增成员";
			vm.roleList = {};
			vm.isUpdate = false;
			vm.user = {};
			vm.getLoginUser();
			//获取角色信息
			this.getRoleList();
		},
		update:function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.isUpdate = true;
			vm.showList = false;
			vm.title = "修改成员";
			vm.getLoginUser();
			vm.getUser(userId);
			//获取角色信息
			this.getRoleList();

		},
		del:function () {
			var userIds = getSelectedRow();
			if(userIds == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({url: "/platform/manage/admindel/"+userIds,
					success: function(r){if(r.code == 200){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		saveOrUpdate:function () {
			var url = !vm.isUpdate ? "/platform/manage/adminadd" : "/platform/manage/adminupdate";
			if(vm.user.password.trim().length<6){
				alert("密码长度不足6位");
				return;
			}
			vm.user.password = hex_md5(vm.user.password.trim());
			var pattern = /^1[34578]\d{9}$/;
			if(!pattern.test(vm.user.phone)){
				alert("手机格式错误");
				return;
			}
			$.ajax({
				type: "POST",
				url: url,
				data: JSON.stringify(vm.user),
				success: function(r){
					if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
		},
		getLoginUser:function () {
			$.ajax({url:"/platform/admininfo",async:false,success:function (r) {
				vm.loginUser= r.result;
			}});
		},
		getUser: function(userId){
			$.ajax({url:"/platform/manage/admininfo/"+userId,async:false,success: function(r){
				vm.user = r.result;

			}});
		},
		getRoleList: function(event){
			if(vm.user.appId){
				$.ajax({url:"/platform/manage/adminrole?appId="+vm.user.appId,async:false,success: function(r){
					vm.roleList = r.result;
				}});
			}else if(vm.loginUser.appId){
				$.ajax({url:"/platform/manage/adminrole?appId="+vm.loginUser.appId,async:false,success: function(r){
					vm.roleList = r.result;
				}});
			}else{
				$.ajax({url:"/platform/manage/adminrole",async:false, success:function(r){
					vm.roleList = r.result;
				}});
			}
		},

		reload: function (event) {
			vm.showList = true;
			vm.isUpdate = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		}
	}
});
/*
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			username: null
		},
		showList: true,
		title:null,
		roleList:{},
		isUpdate:false,
		user:{
			status:1,
			roleIdList:[],
			appId:null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.roleList = {};
            vm.isUpdate = false;
			vm.user = {adminStatus:0,roleId:null};
			//获取角色信息
			this.getRoleList();
		},
		update: function () {
			var userId = getSelectedRow();

			if(userId == null){
				return ;
			}
			vm.isUpdate = true;
			vm.showList = false;
            vm.title = "修改";
			
			vm.getUser(userId);
			//获取角色信息
			this.getRoleList();
		},
		del: function () {
			var userIds = getSelectedRow();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
				    url: "/platform/manage/admindel/"+userIds,
				    success: function(r){
						if(r.code == 200){
							alert('操作成功', function(index){
                                vm.reload();
							});
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = !vm.isUpdate ? "/platform/manage/adminadd" : "/platform/manage/adminupdate";

			if(vm.user.password.trim().length<6){
				alert("密码长度不足6位");
				return;
			}
			vm.user.password = hex_md5(vm.user.password.trim());
			var pattern = /^1[34578]\d{9}$/;
			if(!pattern.test(vm.user.phone)){
				alert("手机格式错误");
				return;
			}
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code == 200){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
		},


		getUser: function(userId){
			$.get("/platform/manage/admininfo/"+userId, function(r){
				vm.user = r.result;

			});
		},


		reload: function (event) {
			vm.showList = true;
            vm.isUpdate = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
		},
		getRoleList: function(event){
			if(vm.user.appId){
				$.get("/platform/manage/adminrole?appId="+vm.user.appId, function(r){
					vm.roleList = r.result;
				});
			}else{
				$.get("/platform/manage/adminrole", function(r){
					vm.roleList = r.result;
				});
			}
		},
	}
});*/
